$(document).ready(function () {
    var path = getRootPath();
    initDepart();


/*
* 条件查询
* */
    $("#btn_search").click(function () {
        $("#btn_search").linkbutton('disable');
        var queryParams = $('#queryForm').serializeJSON();
        queryParams.onLoadSuccess = function (data) {
            $("#btn_search").linkbutton('enable');
        };
        $('#tbl_depart_detail').datagrid('load', queryParams);
    });


//打开增加对话框
    $("#btn_addDepart").click(function () {
        console.log("打开增加对话框");
        $('#dlg_departSave').dialog({title: '增加'});
        $("#dlg_departSave").dialog("open");
    });

    //删除
    $("#btn_delDepart").click(function () {
        var row = $('#tbl_depart_detail').datagrid("getSelected");
        if (null == row) {
            $.messager.alert('警告', '请选中要删除的行', 'warning', function () {
                return false;
            });
        } else {
            $.messager.confirm('确认', '你确定要删除‘' + row.departName + '’么？', function (r) {
                if (r) {
                    $("#btn_delDepart").linkbutton('disable');
                    var url = path + "/sysset/delDepart";
                    var postData = {"departId": row.departId};
                    $.post(url, postData, function (data) {
                        var mess = data.mess;
                        if (mess == "succ") {
                            $.messager.alert('消息', '删除成功');
                            $('#dlg_departSave').dialog('close');
                            $('#tbl_depart_detail').datagrid('load');
                            $("#btn_delDepart").linkbutton('enable');
                        } else {
                            $.messager.alert('警告', mess, 'warning', function () {
                                $("#btn_delDepart").linkbutton('enable');
                            });
                        }
                    });
                }
            });
        }
    });

//对话框本体
    $("#btn_editDepart").click(function () {
        $('#dlg_departSave').dialog({title: '修改'});
        var row = $('#tbl_depart_detail').datagrid("getSelected");
        if (null == row) {
            $.messager.alert('警告', '请选中要修改的行', 'warning', function () {
                return false;
            });
        } else {

            $("#add_departName").textbox('setValue', row.departName);
            $("#add_departPhone").textbox('setValue', row.departPhone);
            $("#add_departCharge").textbox('setValue', row.departCharge);
            $("#add_departAddr").textbox('setValue', row.departAddr);
            $("#add_departExplain").textbox('setValue', row.departExplain);
            ////被选择的部门
            $("#add_departId").val(row.departId);
            $("#dlg_departSave").dialog("open");
        }
    });

    /*
     保存
     */
    $("#btn_saveDepart").click(function () {
        var departName = $("#add_departName").textbox('getValue');
        var departPhone = $("#add_departPhone").textbox('getValue');
        var departCharge = $("#add_departCharge").textbox('getValue');
        if (departName == "") {
            $.messager.alert('警告', '部门名称不能为空!', 'warning', function () {
                $("#add_departName").next('span').find('input').focus();
                return false;
            });
        } else if (departPhone == "") {
            $.messager.alert('警告', '部门电话不能为空!', 'warning', function () {
                $("#add_departPhone").next('span').find('input').focus();
                return false;
            });
        } else if (departCharge == "") {
            $.messager.alert('警告', '部门负责人不能为空!', 'warning', function () {
                $("#add_departCharge").next('span').find('input').focus();
                return false;
            });
        } else {
            $("#btn_saveDepart").linkbutton('disable');
            var departId = $("#add_departId").val()
            var url = "";
            if (departId == "")
                url = path + "/sysset/addDepart";
            else
                url = path + "/sysset/updDepart";
            var postData = $("#saveFormPost").serializeJSON();
            $.post(url, postData, function (data) {
                mess = eval(data).mess;
                if (mess == "succ") {
                    if (departId == "")
                        $.messager.alert('消息', '保存成功');
                    else
                        $.messager.alert('消息', '修改成功');
                    $('#dlg_departSave').dialog('close');
                    $("#btn_saveDepart").linkbutton('enable');
                    $('#tbl_depart_detail').datagrid('load');
                    clearForm();
                } else {
                    $.messager.alert('警告', mess, 'warning', function () {
                        $("#btn_saveDepart").linkbutton('enable');
                    });
                }
            });
        }
    });

    /*
    * 清除对话框内容
    *
    * */
    $("#btn_cancelDepart").click(function () {
        clearForm();
        $("#dlg_departSave").dialog("close");
    });


    function clearForm() {
        $("#add_departName").textbox('setValue', "");
        $("#add_departPhone").textbox('setValue', "");
        $("#add_departCharge").textbox('setValue', "");
        $("#add_departAddr").textbox('setValue', "");
        $("#add_departExplain").textbox('setValue', "");
        $("#add_departId").val("");
    }


    /*
    *
    * 全部查询
    * */
    function initDepart() {
        var url = path + "/sysset/queryDepart";
        //data-options="border:false,singleSelect:true,fit:true,fitColumns:true,rownumbers:true"
        $('#tbl_depart_detail').datagrid({
            border: false,
            singleSelect: true,
            fit: true,
            fitColumns: true,
            rownumbers: true,
            autoRowHeight: false,
            nowrap: true,
            loadMsg: "正在加载，请稍后...",
            striped: true,
            url: url,
            pagination: true,
            pageSize: 10,
            pageNumber: 1,
            columns: [[
                {field: 'departName', title: '部门名称', width: 100},
                {field: 'departCharge', title: '负责人', width: 100},
                {field: 'departPhone', title: '部门电话', width: 80},
                {field: 'departAddr', title: '部门地址', width: 120},
                {field: 'departExplain', title: '部门说明', width: 150}
            ]],
            onBeforeLoad: function (param) {
                var pageNo = param.page;
                delete param.page;
                param.pageNo = pageNo;

                var maxResults = param.rows;
                delete param.rows;
                param.maxResults = maxResults;
            },
            onLoadSuccess: function (data) {
                //alert(data);
            }
        });
    }


})